﻿/* AgileFx Version 2.0
 * An open-source framework for rapid development of applications and services using Microsoft.net
 * Developed by: AgileHead
 * Website: www.agilefx.org
 * This component is licensed under the terms of the Apache 2.0 License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgileFx.MVC.FormValidation
{
    public class Validation
    {
        private string PropertyDisplayText { get; set; }
        private List<ValidationRule> Rules = new List<ValidationRule>();

        public Validation()
        { }

        public Validation(string fieldname)
        {
            this.PropertyDisplayText = fieldname;
        }

        public Validation Required()
        {
            this.Rules.Add(new RequiredFieldRule());
            return this;
        }

        public Validation MinimumLength(int length)
        {
            this.Rules.Add(new MinimumLengthRule(length));
            return this;
        }

        public Validation MaximumLength(int length)
        {
            this.Rules.Add(new MaximumLengthRule(length));
            return this;
        }

        public Validation Email()
        {
            this.Rules.Add(new EmailFieldRule());
            return this;
        }

        public Validation Range(int min, int max)
        {
            this.Rules.Add(new RangeRule(min, max));
            return this;
        }

        public Validation Date()
        {
            this.Rules.Add(new DateFieldRule());
            return this;
        }

        public Validation ConfirmPassword()
        {
            this.Rules.Add(new ConfirmPasswordRule());
            return this;
        }

        public void SetAttributes(Dictionary<string, object> attributes)
        {
            attributes["displayName"] = this.PropertyDisplayText;
            attributes["class"] = attributes.ContainsKey("class") ? string.Format("{0} validated", attributes["class"]) : "validated";
            attributes["validation"] = string.Join(",", (from rule in this.Rules select rule.GetAttributeString()).ToArray());
        }
    }
}
